﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys
{
    public partial class uc_Locomotive_MainBase_TrainPart : DevExpress.XtraEditors.XtraForm
    {
        public uc_Locomotive_MainBase_TrainPart()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_TrainPart_Load(object sender, EventArgs e)
        {
            _LoadGrid("-1");
            _LoadClassGrid();
        }

        private void uc_Locomotive_MainBase_TrainPart_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        private void _LoadGrid(string _TCID)
        {
            string strSQL = "select f_id,f_name,(case (select count(*) from TS_S_T_P_TrainPart_TC where f_del=0 and f_tcid="+_TCID+ " and f_partid=TS_S_T_P_TrainPart.f_id) when 0 then convert(bit,0) else  convert(bit,1) end) as f_ck from TS_S_T_P_TrainPart where f_del=0 order by f_id asc";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void _LoadClassGrid()
        {
            string strSQL = "select * from TS_S_T_P_TrainClass where f_del=0";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_TrainClass.DataSource = dt;
        }
        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            bt_Add.Enabled = false;
            bt_Edit.Enabled = false;
            bt_Del.Enabled = false;
            if (tb_Name.Text.Trim().Length > 0 )
            {
                bt_Add.Enabled = true;
                if (gv_Main.SelectedRowsCount > 0)
                {
                    bt_Edit.Enabled = true;
                    bt_Del.Enabled = true;
                }
            }
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "insert into TS_S_T_P_TrainPart(f_name,f_workerid) values('" + tb_Name.Text.Trim() + "'," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
            TS_Win_Class.MySqlCom(strSQL);
            try
            {
                _LoadGrid(gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString());
            }
            catch
            {
                _LoadGrid("-1");
            }
            tb_Name.Text = "";
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_TrainPart set f_name='" + tb_Name.Text.Trim() + "',f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            try
            {
                _LoadGrid(gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString());
            }
            catch
            {
                _LoadGrid("-1");
            }
            tb_Name.Text = "";
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_TrainPart set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            try
            {
                _LoadGrid(gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString());
            }
            catch
            {
                _LoadGrid("-1");
            }
            tb_Name.Text = "";
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                tb_Name.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_name").ToString();
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gc_TrainClass_Click(object sender, EventArgs e)
        {
            try
            {
                _LoadGrid(gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString());
                bt_Relation.Enabled = true;
            }
            catch
            {
                _LoadGrid("-1");
                bt_Relation.Enabled = false;
            }
        }

        private void bt_Relation_Click(object sender, EventArgs e)
        {
            string _str = "";
            for (int i = 0; i < gv_Main.RowCount; i++)
            {
                if ((bool)gv_Main.GetRowCellValue(i, "f_ck"))
                {
                    _str += gv_Main.GetRowCellValue(i, "f_id").ToString() + ",";
                }
            }
            string strSQL = "update TS_S_T_P_TrainPart_TC set f_del=1,f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_del=0 and f_tcid="+ gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString();
            TS_Win_Class.MySqlCom(strSQL);
            if (_str.Length > 0)
            {
                _str = _str.Substring(0, _str.Length-1);
                strSQL = "insert into TS_S_T_P_TrainPart_TC (f_partid,f_tcid,f_workerid) select f_id as f_partid," + gv_TrainClass.GetRowCellValue(gv_TrainClass.GetSelectedRows()[0], "f_id").ToString()+ " as f_tcid," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + " as f_workerid from TS_S_T_P_TrainPart where f_id in ("+_str+")";
                TS_Win_Class.MySqlCom(strSQL);
            }
            bt_Relation.Enabled = false;
        }
    }
}